#pragma once
#include "tfBondInfo.h"
#include <string>
#include <vector>
#include <qbprotocol/include/SSQBModel.h>
#include <core/sync/lock.h>
#include "tfbondlib.h"

class S_TFBONDLIB_EXPORT CFutureArray{
public:
	std::vector<CFutureInfo> m_data;
};

//过期合约
class S_TFBONDLIB_EXPORT CBondExpireData{
public:
	std::map<std::string, xQBTFAllContractInfo_c> m_mapExpire;
};

class S_TFBONDLIB_EXPORT CBondExpireFuture{
public:
	CBondExpireFuture();
	void GetFutureMap(CBondExpireData& data);
	bool InsertFutureMap(xQBTFAllContractInfoAck_c* pAck);

private:
	CBondExpireData m_ExpireData;
	qb::base::Mutex m_mutex;
};

//国债期货合约
class S_TFBONDLIB_EXPORT CBondFuture{
public:
	CBondFuture();
	virtual ~CBondFuture();
	static CBondFuture& instance();

	bool Clear();
	int	Add(CFutureInfo &newElement);
	bool Load(CStDatabase *pDB, void *cookie=nullptr);

	int	GetSize();
	bool GetFutureInfo(const char *szCode, CFutureInfo *pInfo);
	bool GetFutureInfoByKey(const char *szKey, CFutureInfo *pInfo);
	CFutureArray& GetFutureArray();
	const CFutureArray& GetFutureArray() const;
	void GetFutureArray(CFutureArray& futureArray);
	void SetChangeDate(unsigned int date);
	unsigned int GetChangeDate();
public:
// 	class ScopedLock{
// 		CSPFastLock::Scoped m_scoped;
// 	public:
// 		ScopedLock(CBondFuture& future) : m_scoped(future.m_mutex){}
// 		~ScopedLock(){}
// 	};
protected:
	CFutureArray m_future;
	qb::base::Mutex	m_mutex;
	unsigned int m_unNatChangeDate;//新交割时间使用时间，9：30分开始交易
};

class S_TFBONDLIB_EXPORT CFutureRefInfo{
public:
	char m_TF_ID[8];
	char m_bondKey[33];
	float m_convertFactor;
	CFutureRefInfo() : m_convertFactor(0.0){
		memset(m_TF_ID, 0, sizeof(m_TF_ID));
		memset(m_bondKey, 0, sizeof(m_bondKey));
	}
};

class S_TFBONDLIB_EXPORT CFutureRefList{
public:
	std::list<CFutureRefInfo> m_data;
};

class S_TFBONDLIB_EXPORT CFutureRefListEx{
public:
	std::list<std::string> m_data;
};

class S_TFBONDLIB_EXPORT CFutureRefBond{
public:
	CFutureRefBond();
	virtual ~CFutureRefBond();
	static CFutureRefBond& instance();
	void Clear();
	bool Add(const xQBRefBond_c& refBond);
	CFutureRefList GetRefBondList(const char* tfId);
	bool GetRefBondConvertFactor(const char* tfId, const char* bondKey, double& dFactor);
	CFutureRefListEx GetAllTFRefBondList();	//获取所有5年期合约的可交割券
	CFutureRefListEx GetAllTRefBondList();	//获取所有10年期合约的可交割券
	CFutureRefListEx GetAllTSRefBondList();	//获取所有2年期合约的可交割券

protected:
	std::map<std::string, CFutureRefList> m_mpRefBond;
	qb::base::Mutex m_mutex;
};

struct S_TFBONDLIB_EXPORT CActiveFinBondInfo{
	char m_bondkey[32+1];   //key
    char m_listedmarket[8]; //市场代码
    int m_nTransCount;      //成交笔数

    const bool operator < (const CActiveFinBondInfo& obj) const  {
        return (m_nTransCount > obj.m_nTransCount); 
    }

    CActiveFinBondInfo() : m_nTransCount(0){
        memset(m_bondkey, 0, sizeof(m_bondkey));
        memset(m_listedmarket, 0, sizeof(m_listedmarket));
    }
};

class S_TFBONDLIB_EXPORT CActiveFinBondList{
public:
    std::list<CActiveFinBondInfo> m_List;
};

//活跃金融债
class S_TFBONDLIB_EXPORT CActiveFinBond{
public:
    CActiveFinBond(){}
    virtual ~CActiveFinBond(){}
    void Clear();
    void Sort();
    bool Add(const xQBDealActiveFinancialBondUnit_t& activeFinBond);
    CActiveFinBondList GetActiveFindBondList();
    bool IsActiveFinancialBond(CBondInfo& bi);

protected:
    CActiveFinBondList m_data;
	qb::base::Mutex m_mutex;
};
